package com.traffic.between.adaptor;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ws.rs.PathParam;

import org.apache.log4j.Logger;

import com.gxhl.api.core.model.GDPResponse;
import com.traffic.between.annotation.ChooseProvider;
import com.traffic.between.config.BetweenProperties;
import com.traffic.between.config.OrderInfoContext;
import com.traffic.between.redis.running.BsmOrderResultExecutor;
import com.traffic.between.service.YifxFsServiceImpl;
import com.traffic.between.util.ToolUtils;
import com.traffic.managent.service.order.bean.OrderBean;

import net.sf.json.JSONObject;

@ChooseProvider(channel = "yifxfs")
public class YifxFsAdaptor implements IAdaptor{
	private static Logger log = Logger.getLogger(YifxFsAdaptor.class);
	private static String HOST_CALLBACK_URL = BetweenProperties.CALLBACK_URL;//回调地址
	public void order() {
		OrderBean orderBean = OrderInfoContext.getContext();
		orderBean.setSendTime(new Date());
		String productName =orderBean.getProductCode(); 
		String mobile = orderBean.getPhoneNo();
		String orderid = orderBean.getIds();
		String callbackURL = HOST_CALLBACK_URL+"Yifxfs";
		
		try {
			String result = YifxFsServiceImpl.service.order(productName, mobile, orderid, callbackURL);

			JSONObject json = JSONObject.fromObject(result);
			String code = (String) json.get("code");
			if ("11111".equals(code)) {
				orderBean.setMessage(json.getString("msg"));
				orderBean.setErrorMsg("订购失败");
				orderBean.setStatus("8");
			} else {
				Map<String, String> map;
				List list;
				Map<String, String> resultMap;
				if ("000".equals(code)) {
					
					list = (List) json.get("result");
					resultMap = (Map) list.get(0);
					orderBean.setMessage("订购中");
					orderBean.setErrorMsg("订单已受理");
					orderBean.setStatus("11");
					orderBean.setSuppOrderNo(((String) resultMap.get("msgid")) + "");
				} else {
				
					if (json.containsKey("result")) {
						list = (List) json.get("result");
						resultMap = (Map) list.get(0);
						orderBean.setMessage("订购失败");
						orderBean.setErrorMsg(code + "[" + parseResultCodeToMsg(code) + "]");
						orderBean.setStatus("8");
						orderBean.setSuppOrderNo(((String) resultMap.get("msgid")) + "");
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			orderBean.setMessage(e.getMessage());
			orderBean.setStatus("8");
			BsmOrderResultExecutor.service.setOrder(orderBean);
		}
		BsmOrderResultExecutor.service.setOrder(orderBean);
	}

	
	public void status() {
		OrderBean orderBean = OrderInfoContext.getContext();
		
		String msgid = orderBean.getSuppOrderNo();
		log.info("易分享订购状态查询接口msgid：" + msgid);
		try {
			String result = YifxFsServiceImpl.service.orderStatus(msgid);
			log.info("易分享分省流量订购状态查询接口返回数据为 ：" + result);

			JSONObject json = JSONObject.fromObject(result);
			@SuppressWarnings("unchecked")
			Map<String, String> resultMap = (Map<String, String>) json.get("result");
			String code = (String) resultMap.get("code");
			if ("000".equals(code)) {
				String rechargeStatus = (String) resultMap.get("rechargeStatus");
				String rechargeDesc = (String) resultMap.get("rechargeDesc");

				if (!(ToolUtils.isBlank(rechargeStatus))) {
					Map<String, String> map;
					if ("0".equals(rechargeStatus)) {
				
						orderBean.setMessage("订购成功");
						orderBean.setErrorMsg((rechargeDesc == "") ? "订购成功" : rechargeDesc);
						orderBean.setStatus("7");
						
					} else if ("2".equals(rechargeStatus)) {
					
						orderBean.setMessage("订单已受理");
						orderBean.setErrorMsg((rechargeDesc == "") ? "充值中" : rechargeDesc);
						orderBean.setStatus("1");
					} else if ("3".equals(rechargeStatus)) {
					
						orderBean.setMessage("订购失败");
						orderBean.setErrorMsg((rechargeDesc == "") ? "订单不存在" : rechargeDesc);
						orderBean.setStatus("8");
					} else {
						
						orderBean.setMessage("订购失败");
						orderBean.setErrorMsg((rechargeDesc == "") ? "订购失败" : rechargeDesc);
						orderBean.setStatus("8");
					}
				} else {
					
					orderBean.setMessage("状态查询失败, 查询返回的充值状态rechargeStatus字段值为空");
					orderBean.setStatus("8");
				}
			} else {
				
				orderBean.setMessage("状态查询失败, 返回code字段值为[" + code + "]");
				orderBean.setStatus("8");
			}
		} catch (Exception e) {
			e.printStackTrace();
			orderBean.setMessage(e.getMessage());
			orderBean.setStatus("8");
		}
		BsmOrderResultExecutor.service.setOrder(orderBean);
	}

	public String parseResultCodeToMsg(String code) {
		String msg = "";
		if ("000".equals(code))
			msg = "请求成功";
		else if ("001".equals(code))
			msg = "用户名或密码错误";
		else if ("002".equals(code))
			msg = "管理员不允许调用接口";
		else if ("003".equals(code))
			msg = "调用传入了空参数";
		else if ("004".equals(code))
			msg = "用户IP未绑定";
		else if ("005".equals(code))
			msg = "用户被锁定";
		else if ("006".equals(code))
			msg = "已经超过本月最大充值额度";
		else if ("007".equals(code))
			msg = "已经超过最大充值额度";
		else if ("008".equals(code))
			msg = "包含黑名单内的号码";
		else if ("009".equals(code))
			msg = "未知的号码段";
		else if ("010".equals(code))
			msg = "不支持的流量包";
		else if ("011".equals(code))
			msg = "解析报文出现异常";
		else if ("012".equals(code))
			msg = "调用Order接口提交失败";
		else if ("013".equals(code))
			msg = "参数签名错误";
		else {
			msg = "other fail reason";
		}
		return msg;
	}
}